package com.ujson.chat.server.builder;

import com.ujson.base.common.dto.Res;
import com.ujson.base.common.enums.ErrorCode;
import com.ujson.base.common.utils.EnumUtil;
import com.ujson.chat.common.proto.MessageProto;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.Objects;

/**
 * <h3>ujson-parent</h3>
 * <p>登录结果消息构造器</p>
 *
 * @author JerryLiu  a1031757312@gmail.com
 * @version 1.0
 * @date 2021-11-26 19:44
 */
@Slf4j
@Service
public class LoginResponseBuilder {

    public MessageProto.Message build(Res<Object> rest, ErrorCode errorCode, long sequence) {
        ErrorCode code = errorCode;
        MessageProto.LoginResponse.Builder responseBuilder = MessageProto.LoginResponse.newBuilder();
        responseBuilder.setResult(code.isSuccess());
        responseBuilder.setCode(code.getCode());
        responseBuilder.setInfo(code.getMsg());
        responseBuilder.setExpose(1);
        if (code.isSuccess()) {
            responseBuilder.setSessionId(rest.getData().toString());
        }
        return MessageProto.Message.newBuilder()
                .setType(MessageProto.MessageType.LOGIN_RESPONSE)
                .setSequence(sequence)
                .setLoginResponse(responseBuilder)
                .build();
    }
}
